<?php
/**
**百度直达号对接
**Time 2015-11-02 
**pm 14:00
**User by PZP
**/
include_once '/var/www/html/new/com.inc.php';
class BD_zdh{

	private $bd_account = '504025';
	private $bd_ID = 22196;
	
	public function __construct(){
		$this->inSide = new SoapClient(null,array(
			"location" => "http://112.124.14.164/open/openService/pft_insideMX.php",
			"uri" => "www.16u.com?ac_16u=16ucom|pw_16u=c33367701511b4f6020ec61ded352059|auth_16u=true")
		);
		$this->outSide = new SoapClient("http://open.12301.cc/openService/MXSE.wsdl",array('encoding' =>'UTF-8','cache_wsdl' => 0));
	}
	
	public function Post_bd_Product($tid_aids,$sp_no){
		$tickets2 = array();
		foreach($tid_aids as $v){
			$tids =  explode("|",$v);
			$aid = $tids[0];
			$lid = $tids[1];
			$tid = $tids[2];
			
			$sel_pid = "select pid from uu_jq_ticket where id=$tid limit 1";
			$GLOBALS['le']->query($sel_pid);
			$GLOBALS['le']->fetch_assoc();
			$pid = $GLOBALS['le']->f('pid');
			
			$sql = "select t.delaydays,t.order_start,p.p_status,l.jqts,l.address,l.title as ltitle,t.title as ttitle,t.tprice,t.getaddr,l.runtime,l.jtzn,l.bhjq,l.imgpath from uu_products p left join uu_jq_ticket t on t.pid = p.id left join uu_land l on l.id = t.landid where p.apply_limit=1 and p.p_status in (0,2,3,4,5) and t.pay=1 and p.id=$pid limit 1";

			$GLOBALS['le']->query($sql);
			$str = $GLOBALS['le']->fetch_assoc();
			if(!$str) continue;
			
			if($str['delaydays'] ==0 && $str['order_start']){
				$ticket_valid = $str['order_start'].' 至 '.$str['order_end'];
			}elseif($str['delaydays'] == 0){  
				$ticket_valid = '当天有效';
			}else{
				$ticket_valid = $str['delaydays'].'天内有效';
			}
			if($str['p_status']==0) $ticket_status=1; else $ticket_status=2;
			$ext =json_encode(
					'	预订须知:'.$str['jqts'].
					'	营业时间:'.$str['runtime'].
					'   景区地址:'.$str['address'].
					'	景区简介:'.$str['bhjq'].
					'	交通指南:'.$str['jtzn'].
					'	景区主图:'.$str['imgpath'].
					'   门票有效期:'.$ticket_valid.
					'	入园方式:'.$str['getaddr']
				);
				
			//供应价
			$gyprice = $this->inSide->Dynamic_Price_And_Storage($this->bd_account,$pid,date('Y-m-d'),1,0,1,$aid);
			if($gyprice == 105 || $gyprice ==1065 || $gyprice == 103) continue;
			$gyprice_xml =simpleXML_load_string($gyprice); 
			$gyprice_p = (string)$gyprice_xml->Rec->UUsprice;
			
						
			//零售价
			$lsprice =$this->inSide->Dynamic_Price_And_Storage($this->bd_account,$pid,date('Y-m-d'),1,1,1,$aid); 
			if($lsprice == 105 || $lsprice ==1065 || $lsprice ==103) continue;			
			$lsprice_xml =simpleXML_load_string($lsprice); 
			$lsprice_p = (string)$lsprice_xml->Rec->UUsprice;
			$stock = (int)$lsprice_xml->Rec->UUstorage ;

			$tickets = array(
				't_id'	=> $v,  					//第三方（服务商）门票编号
				'sp_no'	=> $sp_no, 					//百度分配的店铺编号，支付时使用。
				'title'	=> $str['ltitle'].$str['ttitle'], 	//门票名称
				'price'	=> $lsprice_p, 						//实际销售价格
				'original_price'=>$str['tprice']*100,  		//原价
				'supply_price'=>$gyprice_p, 				//供应价格
				'stock' => $stock, 							//库存数量
				'status'=> $ticket_status, 					//票状态
				'ext'	=> $ext, 							//其他数据，是一个k:v对象
			);
			$tickets2 []= $tickets;		
		}
		return $tickets2;
	}
	
	//签名计算
	public function sign32($provider_no,$msg,$time,$secret_key){
		return strtolower(md5(sprintf("%s%s%d%s",$provider_no,$msg,$time,$secret_key)));
	}
	
	public function get_imgUrl($url){
		$code = file_get_contents($url);
		$pattern="/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/";
		preg_match_all($pattern,$code,$match);
		// print_r($match[1][0]);
		return $match[1][0];
	}


	public function bd_zhida_post($get_data){  
		// $RqUrl ='http://180.149.144.147:8173/public/2.0/sku/sync';
		$RqUrl ='https://openapi.baidu.com/public/2.0/sku/sync';
		$str= http_build_query($get_data);
		$this->logfile($str,"post_data");
		$ch = curl_init();//打开
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_URL, $RqUrl);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $str);
		$response  = curl_exec($ch);
		curl_close($ch);//关闭 
		$result = json_decode($response,true);
		return $result;
	}
	
	public function logfile($word,$path){
		$filepath= '/var/www/html/new/d/logs/api/BD_zdh/'.$path.'.txt';
		$word = date("Y-m-d H:i:s")."|".$word;
		if(file_exists($filepath)){
			$maxsize  = 1024*1024*10;
			$filesize = filesize($filepath);
			$filetype = end(explode(".",$filepath));
			$filename = substr($filepath,0,strpos($filepath,'.'));
			if($filesize>$maxsize)
				rename($filepath, $filename.'_'.date('YmdHis').'.'.$filetype);
		}
		$fp = fopen($filepath,"a");
		flock($fp, LOCK_EX);
		fwrite($fp,$word."\n");
		flock($fp, LOCK_UN);
		fclose($fp);
	}
}

// $bd_zdh = new BD_zdh();
// $Post_bd_Product = $bd_zdh->Post_bd_Product(array("113|2633|5715")); 
?>var/www/html/new/d/logs/api/api_order_Dynamic/'.$path.'.txt';
		$word = date("Y-m-d H:i:s")."|13|".$word;
		if(file_exists($filepath)){
			$maxsize  = 1024*1024*10;
			$filesize = filesize($filepath);
			$filetype = end(explode(".",$filepath));
			$filename = substr($filepath,0,strpos($filepath,'.'));
			if($filesize>$maxsize)
				rename($filepath, $filename.'_'.date('YmdHis').'.'.$filetype);
		}
		$fp = fopen($filepath,"a");
		flock($fp, LOCK_EX);
		fwrite($fp,$word."\n");
		flock($fp, LOCK_UN);
		fclose($fp);
	}
}

// $bd_zdh = new BD_zdh();
// $Post_bd_Product = $bd_zdh->Post_bd_Product(array("113|2633|5715")); 
?>